package com.sk.bilibili.model.fragment;

import com.sk.api.annotations.Autowired;
import com.sk.bilibili.bean.Focus;
import com.sk.bilibili.callBack.RequestCallBack;
import com.sk.bilibili.mapper.DiscussMapper;
import com.sk.bilibili.mapper.FocusMapper;
import com.sk.bilibili.mapper.PraiseMapper;
import com.sk.bilibili.model.BaseModel;
import com.sk.bilibili.other.JsonMap;
import com.sk.bilibili.utils.RequestUtils;
import com.sk.bilibili.view.fragment.DiscussFragment;

public class DiscussModel extends BaseModel<DiscussFragment> {

    @Autowired
    DiscussMapper discussMapper;
    @Autowired
    FocusMapper focusMapper;
    @Autowired
    PraiseMapper praiseMapper;
    private int size = 10;
    private int page = 1;

    public void loadDiscuss(Integer targetId, Integer type) {
        JsonMap map = getJsonMap(targetId, type);
        RequestUtils.process(discussMapper.getDiscuss(map), discussPage -> {
            view.addDiscuss(discussPage.getRecords());
            page++;
        });
    }

    private JsonMap getJsonMap(Integer targetId, Integer type) {
        JsonMap map = new JsonMap();
        map.put("page", page);
        map.put("size", size);
        map.put("targetId", targetId);
        map.put("type", type);
        map.put("sort",1);
        return map;
    }

    public void refreshDiscuss(Integer targetId, Integer type) {
        page = 1;
        JsonMap map = getJsonMap(targetId, type);
        RequestUtils.process(discussMapper.getDiscuss(map), discussPage -> {
            view.setDiscuss(discussPage.getRecords());
            page++;
        });
    }

    public void sendDiscuss(String msg, Integer targetId, Integer type) {
        JsonMap map = getJsonMap(targetId, type);
        map.put("comment", msg);
        RequestUtils.process(discussMapper.add(map), unused -> view.sendSuccess());
    }

    public void switchFocus(Integer id) {
        RequestUtils.process(focusMapper.switchFocus(id), focus -> view.switchFocusSuccess(focus!=null));
    }

    public void praise(Integer id) {
        JsonMap map = new JsonMap();
        map.put("type",3);
        map.put("targetId",id);
        RequestUtils.process(praiseMapper.switchPraise(map));
    }

    public void deleteDiscuss(Integer id) {
        RequestUtils.process(discussMapper.delete(id),
                unused -> view.deleteDiscussSuccess());
    }

}